//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "QuantoTermStructure.h"
using namespace Cephei::QL::Termstructures::Yield;
#include <gen/QL/Times/Calendar.h>
#include <gen/QL/Times/DayCounter.h>
#include <gen/QL/Termstructures/YieldTermStructure.h>
#include <gen/QL/Termstructures/Volatility/Equityfx/BlackVolTermStructure.h>
#include <gen/QL/Quote.h>
#include <gen/QL/InterestRate.h>
#include <gen/QL/Times/Period.h>
#include <gen/QL/Termstructures/Yield/ZeroYieldStructure.h>
using namespace Cephei::QL::Times;
using namespace Cephei::QL::Termstructures;
using namespace Cephei::QL::Termstructures::Volatility::Equityfx;
using namespace Cephei::QL;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Termstructures::Yield::CQuantoTermStructure::CQuantoTermStructure (Cephei::QL::Termstructures::IYieldTermStructure^ underlyingDividendTS, Cephei::QL::Termstructures::IYieldTermStructure^ riskFreeTS, Cephei::QL::Termstructures::IYieldTermStructure^ foreignRiskFreeTS, Cephei::QL::Termstructures::Volatility::Equityfx::IBlackVolTermStructure^ underlyingBlackVolTS, Double strike, Cephei::QL::Termstructures::Volatility::Equityfx::IBlackVolTermStructure^ exchRateBlackVolTS, Double exchRateATMlevel, Double underlyingExchRateCorrelation) : CZeroYieldStructure(CQuantoTermStructure::typeid)
{
    CYieldTermStructure^ _CunderlyingDividendTS;
    CYieldTermStructure^ _CriskFreeTS;
    CYieldTermStructure^ _CforeignRiskFreeTS;
    CBlackVolTermStructure^ _CunderlyingBlackVolTS;
    CBlackVolTermStructure^ _CexchRateBlackVolTS;
    try
    {
#ifdef HANDLE
        _phQuantoTermStructure = NULL;
#endif
        _CunderlyingDividendTS = safe_cast<CYieldTermStructure^> (underlyingDividendTS);
        _CunderlyingDividendTS->Lock();
        Handle<QuantLib::YieldTermStructure>& _underlyingDividendTS = static_cast<Handle<QuantLib::YieldTermStructure>&> (_CunderlyingDividendTS->GetHandle ()); 
        _CriskFreeTS = safe_cast<CYieldTermStructure^> (riskFreeTS);
        _CriskFreeTS->Lock();
        Handle<QuantLib::YieldTermStructure>& _riskFreeTS = static_cast<Handle<QuantLib::YieldTermStructure>&> (_CriskFreeTS->GetHandle ()); 
        _CforeignRiskFreeTS = safe_cast<CYieldTermStructure^> (foreignRiskFreeTS);
        _CforeignRiskFreeTS->Lock();
        Handle<QuantLib::YieldTermStructure>& _foreignRiskFreeTS = static_cast<Handle<QuantLib::YieldTermStructure>&> (_CforeignRiskFreeTS->GetHandle ()); 
        _CunderlyingBlackVolTS = safe_cast<CBlackVolTermStructure^> (underlyingBlackVolTS);
        _CunderlyingBlackVolTS->Lock();
        Handle<QuantLib::BlackVolTermStructure>& _underlyingBlackVolTS = static_cast<Handle<QuantLib::BlackVolTermStructure>&> (_CunderlyingBlackVolTS->GetHandle ()); 
        QuantLib::Real _strike = (QuantLib::Real)ValueHelper::Convert (strike);
        _CexchRateBlackVolTS = safe_cast<CBlackVolTermStructure^> (exchRateBlackVolTS);
        _CexchRateBlackVolTS->Lock();
        Handle<QuantLib::BlackVolTermStructure>& _exchRateBlackVolTS = static_cast<Handle<QuantLib::BlackVolTermStructure>&> (_CexchRateBlackVolTS->GetHandle ()); 
        QuantLib::Real _exchRateATMlevel = (QuantLib::Real)ValueHelper::Convert (exchRateATMlevel);
        QuantLib::Real _underlyingExchRateCorrelation = (QuantLib::Real)ValueHelper::Convert (underlyingExchRateCorrelation);
        _ppQuantoTermStructure = new boost::shared_ptr<QuantLib::QuantoTermStructure> (new QuantLib::QuantoTermStructure ( _underlyingDividendTS,  _riskFreeTS,  _foreignRiskFreeTS,  _underlyingBlackVolTS,  _strike,  _exchRateBlackVolTS,  _exchRateATMlevel,  _underlyingExchRateCorrelation ));
        SetZeroYieldStructure (boost::dynamic_pointer_cast<QuantLib::ZeroYieldStructure> (*_ppQuantoTermStructure));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_CunderlyingDividendTS != nullptr) _CunderlyingDividendTS->Unlock();
        if (_CriskFreeTS != nullptr) _CriskFreeTS->Unlock();
        if (_CforeignRiskFreeTS != nullptr) _CforeignRiskFreeTS->Unlock();
        if (_CunderlyingBlackVolTS != nullptr) _CunderlyingBlackVolTS->Unlock();
        if (_CexchRateBlackVolTS != nullptr) _CexchRateBlackVolTS->Unlock();
    }
}
Cephei::QL::Termstructures::Yield::CQuantoTermStructure::CQuantoTermStructure (boost::shared_ptr<QuantLib::QuantoTermStructure>& childNative, Object^ owner) : CZeroYieldStructure(CQuantoTermStructure::typeid)
{
#ifdef HANDLE
	_phQuantoTermStructure = NULL;
#endif
	_ppQuantoTermStructure = &childNative;
    _ppZeroYieldStructure = new boost::shared_ptr<QuantLib::ZeroYieldStructure> (boost::dynamic_pointer_cast<QuantLib::ZeroYieldStructure> (*_ppQuantoTermStructure));
}
Cephei::QL::Termstructures::Yield::CQuantoTermStructure::CQuantoTermStructure (QuantLib::QuantoTermStructure& childNative, Object^ owner) : CZeroYieldStructure(CQuantoTermStructure::typeid)
{
#ifdef HANDLE
	_phQuantoTermStructure = NULL;
#endif
	_ppQuantoTermStructure = new boost::shared_ptr<QuantLib::QuantoTermStructure> (&childNative);
    _ppZeroYieldStructure = new boost::shared_ptr<QuantLib::ZeroYieldStructure> (boost::dynamic_pointer_cast<QuantLib::ZeroYieldStructure> (*_ppQuantoTermStructure));
    _QuantoTermStructureOwner = owner;
    _ZeroYieldStructureOwner = owner;
}

Cephei::QL::Termstructures::Yield::CQuantoTermStructure::CQuantoTermStructure (CQuantoTermStructure^ copy) : CZeroYieldStructure(CQuantoTermStructure::typeid)
{
#ifdef HANDLE
	_phQuantoTermStructure = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppQuantoTermStructure = new boost::shared_ptr<QuantLib::QuantoTermStructure> (copy->GetShared());
        _ppZeroYieldStructure = new boost::shared_ptr<QuantLib::ZeroYieldStructure> (boost::dynamic_pointer_cast<QuantLib::ZeroYieldStructure> (*_ppQuantoTermStructure));
    }
}
Cephei::QL::Termstructures::Yield::CQuantoTermStructure::CQuantoTermStructure (System::Type^ t) : CZeroYieldStructure(CQuantoTermStructure::typeid)
{
#ifdef HANDLE
	_phQuantoTermStructure = NULL;
#endif
	if (!t->IsSubclassOf(CQuantoTermStructure::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Termstructures::Yield::CQuantoTermStructure::CQuantoTermStructure (QuantLib::Handle<QuantLib::QuantoTermStructure>& childNative, Object^ owner)  : CZeroYieldStructure(CQuantoTermStructure::typeid)
{
	_phQuantoTermStructure = &childNative;
	_ppQuantoTermStructure = &static_cast<boost::shared_ptr<QuantLib::QuantoTermStructure>>(childNative.currentLink());
    _ppZeroYieldStructure = new boost::shared_ptr<QuantLib::ZeroYieldStructure> (boost::dynamic_pointer_cast<QuantLib::ZeroYieldStructure> (*_ppQuantoTermStructure));
    _QuantoTermStructureOwner = owner;
}
Cephei::QL::Termstructures::Yield::CQuantoTermStructure::CQuantoTermStructure (QuantLib::Handle<QuantLib::QuantoTermStructure> childNative)  : CZeroYieldStructure(CQuantoTermStructure::typeid)
{
	_phQuantoTermStructure = &childNative;
	_ppQuantoTermStructure = &static_cast<boost::shared_ptr<QuantLib::QuantoTermStructure>>(childNative.currentLink());
    _ppZeroYieldStructure = new boost::shared_ptr<QuantLib::ZeroYieldStructure> (boost::dynamic_pointer_cast<QuantLib::ZeroYieldStructure> (*_ppQuantoTermStructure));
}
#endif
#ifdef STRUCT
Cephei::QL::Termstructures::Yield::CQuantoTermStructure::CQuantoTermStructure (QuantLib::QuantoTermStructure childNative)  : CZeroYieldStructure(CQuantoTermStructure::typeid)
{
#ifdef HANDLE
	_phQuantoTermStructure = NULL;
#endif
	_ppQuantoTermStructure = new boost::shared_ptr<QuantLib::QuantoTermStructure> (new QuantLib::QuantoTermStructure (childNative));
    _ppZeroYieldStructure = new boost::shared_ptr<QuantLib::ZeroYieldStructure> (boost::dynamic_pointer_cast<QuantLib::ZeroYieldStructure> (*_ppQuantoTermStructure));
}
#endif

Cephei::QL::Termstructures::Yield::CQuantoTermStructure::~CQuantoTermStructure ()
{
    if (_ppQuantoTermStructure != NULL)
    {
	    delete _ppQuantoTermStructure;
        _ppQuantoTermStructure = NULL;
    }
}
Cephei::QL::Termstructures::Yield::CQuantoTermStructure::!CQuantoTermStructure ()
{
    if (_ppQuantoTermStructure != NULL)
    {
	    delete _ppQuantoTermStructure;
    }
}
QuantLib::QuantoTermStructure& Cephei::QL::Termstructures::Yield::CQuantoTermStructure::GetReference ()
{
    if (_ppQuantoTermStructure == NULL) throw gcnew NativeNullException ();
	return **_ppQuantoTermStructure;
}
boost::shared_ptr<QuantLib::QuantoTermStructure>& Cephei::QL::Termstructures::Yield::CQuantoTermStructure::GetShared ()
{
    if (_ppQuantoTermStructure == NULL) throw gcnew NativeNullException ();
	return *_ppQuantoTermStructure;
}
QuantLib::QuantoTermStructure* Cephei::QL::Termstructures::Yield::CQuantoTermStructure::GetPointer ()
{
    if (_ppQuantoTermStructure == NULL) throw gcnew NativeNullException ();
	return &**_ppQuantoTermStructure;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::QuantoTermStructure>& Cephei::QL::Termstructures::Yield::CQuantoTermStructure::GetHandle ()
{
	if (_phQuantoTermStructure == NULL)
	{
		_phQuantoTermStructure = new Handle<QuantLib::QuantoTermStructure> (*_ppQuantoTermStructure);
	}
	return *_phQuantoTermStructure;
}
#endif
bool Cephei::QL::Termstructures::Yield::CQuantoTermStructure::HasNative () 
{
	return (_ppQuantoTermStructure != NULL);
}

Cephei::QL::Times::ICalendar^ Cephei::QL::Termstructures::Yield::CQuantoTermStructure::Calendar::get ()
{
    try
    {
    	QuantLib::Calendar _rv = (QuantLib::Calendar)(*_ppQuantoTermStructure)->calendar ( );   
        Cephei::QL::Times::CCalendar^ _nrv = gcnew Cephei::QL::Times::CCalendar (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Times::IDayCounter^ Cephei::QL::Termstructures::Yield::CQuantoTermStructure::DayCounter::get ()
{
    try
    {
    	QuantLib::DayCounter _rv = (QuantLib::DayCounter)(*_ppQuantoTermStructure)->dayCounter ( );   
        Cephei::QL::Times::CDayCounter^ _nrv = gcnew Cephei::QL::Times::CDayCounter (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
DateTime Cephei::QL::Termstructures::Yield::CQuantoTermStructure::MaxDate::get ()
{
    try
    {
    	QuantLib::Date _rv = (QuantLib::Date)(*_ppQuantoTermStructure)->maxDate ( );   
        DateTime _nrv = (DateTime)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
DateTime Cephei::QL::Termstructures::Yield::CQuantoTermStructure::ReferenceDate::get ()
{
    try
    {
    	QuantLib::Date _rv = (QuantLib::Date)(*_ppQuantoTermStructure)->referenceDate ( );   
        DateTime _nrv = (DateTime)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
UInt32 Cephei::QL::Termstructures::Yield::CQuantoTermStructure::SettlementDays::get ()
{
    try
    {
    	QuantLib::Natural _rv = (QuantLib::Natural)(*_ppQuantoTermStructure)->settlementDays ( );   
        UInt32 _nrv = (UInt32)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Termstructures::Yield::IQuantoTermStructure^ Cephei::QL::Termstructures::Yield::CQuantoTermStructure_Factory::Create (Cephei::QL::Termstructures::IYieldTermStructure^ underlyingDividendTS, Cephei::QL::Termstructures::IYieldTermStructure^ riskFreeTS, Cephei::QL::Termstructures::IYieldTermStructure^ foreignRiskFreeTS, Cephei::QL::Termstructures::Volatility::Equityfx::IBlackVolTermStructure^ underlyingBlackVolTS, Double strike, Cephei::QL::Termstructures::Volatility::Equityfx::IBlackVolTermStructure^ exchRateBlackVolTS, Double exchRateATMlevel, Double underlyingExchRateCorrelation)
{
    return gcnew CQuantoTermStructure ( underlyingDividendTS,  riskFreeTS,  foreignRiskFreeTS,  underlyingBlackVolTS,  strike,  exchRateBlackVolTS,  exchRateATMlevel,  underlyingExchRateCorrelation);
}
